<!-- 瓦斯突出报警 -->
<template>
  <div class="pagestyle">
    <div class="addBox" v-if="addShow">
      <stepcom :stepNum="stepNum"></stepcom>
      <div v-if="stepNum == 1" style="text-align: center; height: 64vh;
      width: 500px;margin: 80px auto;">
        <el-form ref="ruleFormRef" :model="addForm" :rules="state.formListRules" label-position="top"
          label-width="auto">
          <el-form-item label="设置瓦斯突出报警断电名称" prop="gasProtrusionName">
            <el-input v-model="addForm.gasProtrusionName" placeholder="请输入" />
          </el-form-item>
          <div style="display: flex;">
            <el-form-item label="是否下发分站" prop="isPublish">
              <el-select style="width: 240px;" v-model="addForm.isPublish" placeholder="请选择"
                @change="isPublishStepOneChange">
                <el-option v-for="item in [{label:'是',value:true},{label:'否',value:false}]" :label="item.label"
                  :value="item.value" />
              </el-select>
            </el-form-item>
            <el-form-item label="地点类型" prop="placeType" style="margin-left: 10px;" v-if="addForm.isPublish==1">
              <el-select style="width: 250px;" v-model="addForm.placeType" placeholder="请选择">
                <el-option v-for="item in ['掘进工作面','采煤工作面']" :label="item" :value="item" />
              </el-select>
            </el-form-item>
          </div>
          <p style="color: rgba(53, 140, 240, 1);text-align: left;">注:瓦斯突出报警如果不下发分站，则只可配置1条判断公式！</p>
          <div>
            <el-form-item label="选择分站" prop="stationCode" v-if="addForm.isPublish==1">
              <el-select @change="selstationCodechange" style="width: 250px;" v-model="addForm.stationCode" filterable
                placeholder="请选择">
                <el-option v-for="item in state.stationCodeList" :label="item.label" :value="item.value">
                </el-option>
              </el-select>
            </el-form-item>
          </div>
        </el-form>
        <div class="btnSubmit">
          <el-button type="info" @click="stepNumClick(9,false)">取消</el-button>
          <el-button type="primary" @click="stepNumClick(2, true)">下一步</el-button>
        </div>
      </div>
      <div v-if="stepNum == 2 || stepNum == 3">
        <p class="p_title">详细信息 <el-button type="text" style="margin-left: 10px;">修改</el-button>
        </p>
        <div style="display: flex;justify-content: start;font-size: 14px;background-color: rgba(173, 173, 173, 0.12);
        border-radius: 4px;margin: 0 10px;padding: 0 16px;">
          <p>瓦斯突出报警断电名称:{{ addForm.gasProtrusionName }}</p>
          <p style="margin-left: 150px;">是否下发分站:{{ addForm.isPublish==1?'是':'否' }}</p>
          <p style="margin-left: 150px;" v-if="addForm.isPublish==1">地点类型:{{ addForm.placeType }}</p>
        </div>
      </div>
      <div v-if="stepNum == 2" style="margin-top: 40px">
        <p class="p_title">查询测点</p>
        <div style="
            background: rgba(91, 109, 128, 0.04);
            margin: 0 10px;
            padding: 10px;
          ">
          <div style="display: flex;align-items: center;">
            <el-form-item label="分站号">
              <el-select @change="querystationCodechange" style="width: 250px;" v-model="addForm.conditionStationCode"
                filterable placeholder="请选择">
                <el-option v-for="item in state.stationCodeList" :label="item.label" :value="item.value">
                </el-option>
              </el-select>
            </el-form-item>
            <el-form-item style="margin-left: 10px;" label="搜索">
              <el-input v-model="addForm.placeName"></el-input>
            </el-form-item>
            <el-form-item style="margin-left: 10px;">
              <el-button type="info" @click="queryClick(2)">重置</el-button>
              <el-button type="primary" @click="queryClick(1)">搜索</el-button>
            </el-form-item>
          </div>
          <vxe-grid ref="gridRef" v-bind="{
          columnConfig: {
              resizable: true
          },
          border: true,
          showOverflow: true,
          height: 200,
          loading: false,
          scrollY: {
            enabled: true,
            gt: 0
          },
          columns: [
            { type: 'seq', title: '序号', width: 100 },
            { field: 'pointId', title: '测点编号', minWidth: 180 },
            { field: 'sensorName', title: '名称', minWidth: 180 },
            { field: 'placeName', title: '地点', minWidth: 180 },
            { title: '操作', slots: { default: 'action' } }
          ],
          data:listAnalog
          }">
            <template #action="{ row,rowIndex }">
              <span style="font-size: 14px;color: #409eff;cursor: pointer;" @click="selectClick(row,rowIndex)">选择</span>
            </template>
          </vxe-grid>
        </div>
        <p class="p_title">已选因子({{ yinziList.length }})</p>
        <yincard :gaojiBtnShow="true" :cardList="yinziList" 
        :biaodaList="state.biaodaList" @cardListremove="cardListremove"></yincard>
        <div class="btnSubmit">
          <el-button type="info" @click="stepNumClick(1, false)">上一步</el-button>
          <el-button type="primary" @click="stepNumClick(3, false)">下一步</el-button>
        </div>
      </div>
      <div v-if="stepNum == 3">
        <p class="p_title">已选因子({{ yinziList.length }})</p>
        <div style="
            background: rgba(91, 109, 128, 0.04);
            margin: 0 10px;
            padding: 10px;
          ">
          <vxe-table height="200" :data="yinziList" border="inner">
            <vxe-column field="pointId" title="测点编号"></vxe-column>
            <vxe-column field="sensorName" title="设备名称"></vxe-column>
            <vxe-column field="placeName" title="地点"></vxe-column>
            <vxe-column field="cutType" title="断电模式">
              <template #default="{ row }">
                <span style="font-size: 12px; color: red">*</span>
                <el-select style="margin-left: 4px; width: 90%" @change="alarmModelChange($event,row)" clearable
                  filterable v-model="row.cutType" placeholder="请选择">
                  <el-option v-for="item in row.alarmModelList" :key="item" :label="item" :value="item">
                  </el-option>
                </el-select>
              </template>
            </vxe-column>
            <vxe-column field="upperCutValue" title="上限断电值">
              <template #default="{ row }">
                <el-input oninput="value =value.replace(/[^\-\d.]/g, '') .replace(/^\./g, '').replace(/\.{2,}/g,
'.').replace(/(\.\d+)\./g, '$1') .replace(/(-)\./g, '$1') .replace(/\-{2,}/g, '-')
.replace(/(\d+|\.)-/g, '$1') .replace(/-(0){2,}/g,'$1') .replace(/(-)0+(\d+)/g, '$1$2')
.replace(/^0+(\d)/, '$1') .replace(/(\d{8})\d*/, '$1') .replace(/(\.\d{2})\d*/, '$1')"
                  :disabled="row.cutType=='下限断电'||row.type=='D'" style="margin-left: 4px; width: 90%"
                  v-model="row.upperCutValue" placeholder="请输入">
                </el-input>
              </template>
            </vxe-column>
            <vxe-column field="upperRelieveValue" title="上限复电值">
              <template #default="{ row }">
                <el-input oninput="value =value.replace(/[^\-\d.]/g, '') .replace(/^\./g, '').replace(/\.{2,}/g,
'.').replace(/(\.\d+)\./g, '$1') .replace(/(-)\./g, '$1') .replace(/\-{2,}/g, '-')
.replace(/(\d+|\.)-/g, '$1') .replace(/-(0){2,}/g,'$1') .replace(/(-)0+(\d+)/g, '$1$2')
.replace(/^0+(\d)/, '$1') .replace(/(\d{8})\d*/, '$1') .replace(/(\.\d{2})\d*/, '$1')" :disabled="row.cutType=='下限断电'||(row.sensorName=='风速'
                &&row.primaryIndexId==2)||row.type=='D'" style="margin-left: 4px; width: 90%"
                  v-model="row.upperRelieveValue" placeholder="请输入">
                </el-input>
              </template>
            </vxe-column>
            <vxe-column field="lowerCutValue" title="下限断电值">
              <template #default="{ row }">
                <el-input oninput="value =value.replace(/[^\-\d.]/g, '') .replace(/^\./g, '').replace(/\.{2,}/g,
'.').replace(/(\.\d+)\./g, '$1') .replace(/(-)\./g, '$1') .replace(/\-{2,}/g, '-')
.replace(/(\d+|\.)-/g, '$1') .replace(/-(0){2,}/g,'$1') .replace(/(-)0+(\d+)/g, '$1$2')
.replace(/^0+(\d)/, '$1') .replace(/(\d{8})\d*/, '$1') .replace(/(\.\d{2})\d*/, '$1')"
                  :disabled="row.cutType=='上限断电'||row.type=='D'" style="margin-left: 4px; width: 90%"
                  v-model="row.lowerCutValue" placeholder="请输入">
                </el-input>
              </template>
            </vxe-column>
            <vxe-column field="lowerRelieveValue" title="下限复电值">
              <template #default="{ row }">
                <el-input oninput="value =value.replace(/[^\-\d.]/g, '') .replace(/^\./g, '').replace(/\.{2,}/g,
'.').replace(/(\.\d+)\./g, '$1') .replace(/(-)\./g, '$1') .replace(/\-{2,}/g, '-')
.replace(/(\d+|\.)-/g, '$1') .replace(/-(0){2,}/g,'$1') .replace(/(-)0+(\d+)/g, '$1$2')
.replace(/^0+(\d)/, '$1') .replace(/(\d{8})\d*/, '$1') .replace(/(\.\d{2})\d*/, '$1')" :disabled="row.cutType=='上限断电'||(row.sensorName=='风速'
                &&row.primaryIndexId==2)||row.type=='D'" style="margin-left: 4px; width: 90%"
                  v-model="row.lowerRelieveValue" placeholder="请输入">
                </el-input>
              </template>
            </vxe-column>
            <vxe-column field="isCut" title="断线">
              <template #default="{ row }">
                <el-switch v-model="row.isCut"></el-switch>
              </template>
            </vxe-column>
            <vxe-column field="mutationThreshold" title="突变门限">
              <template #default="{ row }">
                <el-input :disabled="row.sensorName!='瓦斯'
                &&row.primaryIndexId!=4" oninput="value =value.replace(/[^\-\d.]/g, '') .replace(/^\./g, '').replace(/\.{2,}/g,
'.').replace(/(\.\d+)\./g, '$1') .replace(/(-)\./g, '$1') .replace(/\-{2,}/g, '-')
.replace(/(\d+|\.)-/g, '$1') .replace(/-(0){2,}/g,'$1') .replace(/(-)0+(\d+)/g, '$1$2')
.replace(/^0+(\d)/, '$1') .replace(/(\d{8})\d*/, '$1') .replace(/(\.\d{2})\d*/, '$1')"
                  style="margin-left: 4px; width: 90%" v-model="row.mutationThreshold" placeholder="请输入">
                </el-input>
              </template>
            </vxe-column>
            <vxe-column field="rise" title="涨幅">
              <template #default="{ row }">
                <el-input :disabled="row.sensorName!='瓦斯'
                &&row.primaryIndexId!=4" oninput="value =value.replace(/[^\-\d.]/g, '') .replace(/^\./g, '').replace(/\.{2,}/g,
'.').replace(/(\.\d+)\./g, '$1') .replace(/(-)\./g, '$1') .replace(/\-{2,}/g, '-')
.replace(/(\d+|\.)-/g, '$1') .replace(/-(0){2,}/g,'$1') .replace(/(-)0+(\d+)/g, '$1$2')
.replace(/^0+(\d)/, '$1') .replace(/(\d{8})\d*/, '$1') .replace(/(\.\d{2})\d*/, '$1')"
                  style="margin-left: 4px; width: 90%" v-model="row.rise" placeholder="请输入">
                </el-input>
              </template>
            </vxe-column>
            <vxe-column field="timeScale" title="时间范围(秒)">
              <template #default="{ row }">
                <el-input oninput="value=value.replace(/[^\d]/g,'')" :disabled="row.sensorName!='瓦斯'
                &&row.primaryIndexId!=4" style="margin-left: 4px; width: 90%" v-model="row.timeScale"
                  placeholder="请输入">
                </el-input>
              </template>
            </vxe-column>
            <vxe-column title="操作" :min-width="80">
              <template #default="{ row,$rowIndex }">
                <el-button type="text" @click="selectHBClick(row)">选择</el-button>
                <el-button type="text" @click="removeHBClick($rowIndex)">移除</el-button>
              </template>
            </vxe-column>
          </vxe-table>
        </div>
        <yunsuanfu jiaoyan="gasProtrusionAlarm/checkExpression"
         :yinziList="state.yinziListHB" @threeStep="threeStep"
         :xiugaiPointList="state.xiugaiPointList"
          :expression="state.expression" :stepSave="stepSave" :biaodaList="state.biaodaList">
        </yunsuanfu>
        <p class="p_title">控制口</p>
        <div style="
            background: rgba(91, 109, 128, 0.04);
            margin: 0 10px;
            padding: 10px;
          ">
          <el-row :gutter="20">
            <el-col :span="6">
              <el-form-item label="分站号" prop="stationNumber">
                <el-select :disabled="addForm.isPublish==1" clearable filterable v-model="addForm.stationNumber"
                  @change="stationNumberChange" placeholder="请选择">
                  <el-option v-for="item in state.stationCodeList" :key="item.label" :label="item.label"
                    :value="item.value">
                  </el-option>
                </el-select>
              </el-form-item>
            </el-col>
            <el-col :span="6">
              <el-button type="info" @click="qingkongClick">清空</el-button>
            </el-col>
            <el-col :span="24">
              <el-checkbox-group v-model="addForm.controlPort" @change="checkboxChange">
                <el-checkbox v-for="item in state.controlPortList"
                  @change="(checked) => handleSingleChange(checked, item)" :label="item" :value="item" />
              </el-checkbox-group>
            </el-col>
            <el-col :span="24">
              <el-input disabled :rows="3" v-model="combinationCutDto.expression" type="textarea" placeholder="请输入">
              </el-input>
            </el-col>
          </el-row>
        </div>
        <p style="text-align: right;margin-top: 10px;margin-right: 10px;">
          <el-button type="primary" @click="saveControlClick">保存控制逻辑</el-button>
        </p>
        <p class="p_title">控制逻辑</p>
        <vxe-table height="200" :data="addForm.controlList" border="inner">
          <vxe-column field="expression" title="判断公式"></vxe-column>
          <vxe-column field="controlExpression" title="断电控制口"></vxe-column>
          <vxe-column field="placeName" title="操作">
            <template #default="{ row,$rowIndex }">
              <el-button type="text" @click="xiugaiClick(row,$rowIndex)">修改</el-button>
              <el-button type="text" @click="yichuClick($rowIndex)">移除</el-button>
            </template>
          </vxe-column>
        </vxe-table>
        <div class="btnSubmit">
          <el-button type="info" @click="stepNumClick(2, false)">上一步</el-button>
          <el-button type="primary" @click="stepNumClick(4, false)">保存</el-button>
        </div>
      </div>
    </div>
    <list_controller ref="zujianname" :options="state.options" v-else :noprintDownShow="true">
      <template v-slot:opraCurdBtnlist="row">
        <span>
          <el-button class="tableFontSize" type="primary" link size="small" @click="editBtn(row.row.row)">编辑
          </el-button>
          <el-button class="tableFontSize" type="primary" link size="small" @click="deleteBtn(row.row.row)">删除
          </el-button>
        </span>
      </template>
      <template v-slot:opraCurdBtnAll>
        <span :class="addShow ? 'listInfoBtn active' : 'listInfoBtn'" @click="addBtn('mul')">
          <svg-icon :class="addShow ? 'active' : ''" class-name="search-icon" icon-class="xinzeng" />
          新增
        </span>
        <span :class="'listInfoBtn'" @click="deleteBtn('mul')">
          <svg-icon class-name="search-icon" icon-class="shanchu" />
          删除
        </span>
      </template>
      <template v-slot:listSS="{ row, field }">
        <span v-if="field == 'isPublish'">
          {{ row.isPublish?'是':'否' }}
        </span>
      </template>
      <template v-slot:selectNamelist> 瓦斯突出报警 </template>
    </list_controller>
  </div>
</template>
<script setup>
  import list_controller from "@/components/commonCom/list_controller";
  import stepcom from "./zuheCom/stepcom";
  import yincard from "./zuheCom/yincard";
  import yunsuanfu from "./zuheCom/yunsuanfu";

  import {
    computed,
    ref,
    reactive
  } from "vue";
  import {
    ElMessage,
    ElMessageBox

  } from "element-plus";
  import {
    table_headers
  } from "@/components/commonCom/table_headers";
  import axios from "@/utils/request";
  import usePagesStore from "@/store/modules/pages";
  import {
    getlistAnalogs
  } from "@/api/config";
  import _ from "lodash";
  const {
    proxy
  } = getCurrentInstance();
  const store = usePagesStore();
  const router = useRouter();
  const stepNum = ref(1);
  const addForm = ref({
    status: "F",
    isCut: false,
    controlList: []
  });
  const addShow = ref(false);
  const yinziList = ref([]);
  const editId = ref(undefined);
  const stepSave = ref(undefined);
  const listAnalog = ref([])
  let state = reactive({
    xiugaiPointList:[],
    controlPortList: [1, 2, 3, 4, 5, 6],
    stationCodeList: [],
    expressStrString1: [],
    stepNum: 1,
    modalTitle: "因子高级配置",
    addModal: false,
    addShow: true,
    biaodaList: [{
        name: "与",
        sub: "&&",
      },
      {
        name: "或",
        sub: "||",
      },
      {
        name: "左括号",
        sub: "(",
      },
      {
        name: "右括号",
        sub: ")",
      },
      {
        name: "撤",
        sub: "销",
      },
      {
        name: "清",
        sub: "空",
      },
    ],
    querystwo: [{
        field: "conditionStationCode",
        title: "分站",
        type: "select",
        binddata: "",
        options: []
      },
      {
        field: "placeName",
        title: "搜索",
        type: "input",
        binddata: "",
      },
    ],
    listAnalog: [],
    tableList: [],
    options: {
      tableheads: computed(() => table_headers.gasProtrusionAlarm),
      listSS: [{
        field: "isPublish"
      }],
      queryList: {
        dropdownUrl: "",
        querys: [],
        queryMap: new Map([
          ["sensorTypeId", "safety_pointType"],
          ["deviceTypeId", "safety_deviceType"],
          ["alarmTypeId", "alarmType"],
        ]),
      },
      dataUrl: "/gasProtrusionAlarm/list",
      delUrl: "/gasProtrusionAlarm/batchDelete",
      addUrl: "/gasProtrusionAlarm/addRecord",
      descUrl: "/gasProtrusionAlarm/detail",
      editUrl: "/gasProtrusionAlarm/modifyRecord",
    },
    formList: [{
      field: "logicAlarmName",
      title: "设置逻辑报警名称",
      type: "input",
      binddata: "",
    }],
    formListRules: {
      gasProtrusionName: [{
        required: true,
        message: "请输入",
        trigger: "blur",
      }],
      placeType: [{
        required: true,
        message: "请选择",
        trigger: "change",
      }],
      stationCode: [{
        required: true,
        message: "请选择",
        trigger: "change",
      }],
    },
    addForm: {
      status: "F",
    },
    yinziList: [],
    yinziListHB: [],
    expression: [],
    stepSave: undefined,
    alarmModelList: ["上限断电", "下限断电", "上下限断电"],
    durationSecondsList: [
      0, 1, 3, 5, 10, 15, 20, 30, 60, 120, 180, 240, 300, 360, 420, 480,
    ],
    disYZObj: {},
    controlListcun:[],
    controlListpush:[]
  });

  function isPublishStepOneChange(val) {
    if (!val) {
      addForm.value.placeType = undefined
      addForm.value.stationCode = undefined
    }
  }
  let combinationCutDto = reactive({
    index: undefined,
    expression: '',
    controlList: []
  })
  // 判断空字符、不存在、为0
  const isEmpty = (value) => value === null || value === undefined || value === "";

  function stationNumberChange(e) {
    addForm.value.conditionStationCode = addForm.value.stationCode
    state.stationCodeList.forEach(item => {
      if (e == item.value) {
        state.controlPortList = item.controlPortList
        addForm.value.controlPort = item.e
        combinationCutDto.controlList.push({
          stationCode: e,
          controlPointDtoList: []
        })
      }
    })
    console.log(combinationCutDto.controlList,"combinationCutDto.controlList")
  }
  // 单个复选框选中取消事件
  function handleSingleChange(checked, item) {
    if (!checked) {
      state.expressStrString1 = state.expressStrString1.filter(itemt => itemt !== Number(addForm.value.stationNumber) +
        '.' + item)
    } else {
      state.expressStrString1.push(Number(addForm.value.stationNumber) + '.' + item)
    }
    combinationCutDto.expression = state.expressStrString1.join('&')
  }

  function addBtn() {
    addShow.value = true;
    editId.value = undefined;
    stepNum.value = 1;
    addForm.value = {
      status: "F",
    };
    state.formList.forEach((item) => {
      item.binddata = undefined;
    });
    state.yinziListHB = [];
    state.expression = undefined;
    yinziList.value = []
    combinationCutDto.expression=undefined
  }

  function alarmModelChange(val, row) {
    if (val == '上限断电') {
      row.lowerCutValue = null
      row.lowerRelieveValue = null
    } else if (val == '下限断电') {
      row.upperCutValue = null
      row.upperRelieveValue = null
    }
  }

  async function editBtn(row) {
    editId.value = row.id;
    addShow.value = true;
    stepNum.value = 1;
    await axios
      .post(state.options.descUrl, {
        id: row.id,
      })
      .then((res) => {
        addForm.value = res.data;
        addForm.value.stationNumber=res.data.stationCode
        addForm.value.stationCodecun=res.data.stationCode
        state.formList.forEach((item) => {
          if (res.data != null) {
            item.binddata = res.data.logicAlarmName;
          }
        });
        let arr = [];
        if (res.data.nodeList.length > 0) {
          res.data.nodeList.forEach((item) => {
            if (item.pointId && item.pointId.includes('A')) {
              item.alarmModelList = ['上限断电', '下限断电', '上下限断电']
              item.alarmModelList = ['上限断电', '下限断电', '上下限断电']
              if (item.sensorName == '风速' && item.primaryIndexId == 2) {
                item.alarmModelList = ['上下限断电']
              }
            } else {
              item.alarmModelList = [item.highShow + '断电', item.lowShow + '断电']
            }
            item.code = item.pointId
            item.objarr = [{
                left: "编号",
                right: item.pointId,
              },
              {
                left: "名称",
                right: item.pointId + "/" + item.sensorName,
              },
              {
                left: "位置",
                right: item.placeName,
              },
            ];
            arr.push(item);
            if (item.place == '-') {
              item.title = item.pointId;
            } else {
              item.title = item.pointId + "/" + item.sensorName;
            }
          });
        }
        state.expression = '';
        addForm.value.controlPort = []
        state.yinziListHB = [];
        yinziList.value = arr
      });
  }


  function threeStep(list) {
    if (list.pointDtoList.length > 0) {
      addForm.value.nameDtoList = list.pointDtoList.map(item => {
        return item.code
      })
    }
    addForm.value.expression = list.expression;
    addForm.value.nodeList = []
    let arr = JSON.parse(JSON.stringify(yinziList.value))
    addForm.value.nodeList = arr
  }

  function getlistGasProtrusionNode() {
    listAnalog.value=[]
    axios.post('gasProtrusionAlarm/listGasProtrusionNode', addForm.value).then(res => {
      if(yinziList.value.length>0){
        yinziList.value.forEach(item=>{
          if(res.data.length>0){
            res.data.forEach(itemsub=>{
              if(item.pointId!=itemsub.pointId){
                listAnalog.value.push(itemsub)
              }
            })
          }
        })
      }else{
        listAnalog.value = res.data
      }
    })
  }

  function selstationCodechange(val) {
    if(addForm.value.stationCodecun!=val){
      yinziList.value=[]
    }
    addForm.value.stationCodecun = val
    addForm.value.stationNumber = val
    addForm.value.conditionStationCode = val
    combinationCutDto.controlList.push({
      stationCode: val,
      controlPointDtoList: []
    })
  }

  function stepNumClick(type, state) {
    if (type == 2 && state) {
      proxy.$refs["ruleFormRef"].validate((valid, fields) => {
        if (valid) {
          stepNum.value = type;
          getlistGasProtrusionNode()
          listAnalog.value = []
        } else {
          stepNum.value = 1;
        }
      })
    } else {
      if (type == 3) {
        if (yinziList.value.length == 0) {
          ElMessage.warning("请先选择测点因子");
        } else {
          if (yinziList.value.length > 0) {
            yinziList.value.forEach(item => {
              if (item.type == 'A') {
                item.alarmModelList = ['上限断电', '下限断电', '上下限断电']
                if (item.sensorName == '风速' && item.primaryIndexId == 2) {
                  item.alarmModelList = ['上下限断电']
                }
              } else {
                item.alarmModelList = [item.highShow + '断电', item.lowShow + '断电']
              }
            })
          }
          stepNum.value = type;
        }
      } else {
        stepNum.value = type;
      }
      if (type == 4) {
        if (addForm.value.controlList && addForm.value.controlList.length > 0) {
          stepSave.value = 4;
          if (editId.value) {
            addForm.value.id = editId.value;
            axios
              .post("/gasProtrusionAlarm/modifyRecord", addForm.value)
              .then((res) => {
                addShow.value = false;
                if (res.code == 200) {
                  ElMessage.success(res.data);
                }
              })
              .catch((err) => {
                stepNum.value = 3
                addShow.value = true;
              });
          } else {
            axios
              .post("/gasProtrusionAlarm/addRecord", addForm.value)
              .then((res) => {
                addShow.value = false;
                if (res.code == 200) {
                  ElMessage.success(res.data);
                }
              })
              .catch((err) => {
                stepNum.value = 3
                addShow.value = true;
              });
          }
        } else {
          stepNum.value = 3
          ElMessage.warning('请先保存逻辑控制!')
        }
      }
      if (type == 9) {
        addShow.value = false
        state.formData = {}
        stepNum.value = 1
        yinziList.value = []
        state.yinziListHB = []
        state.expression = ''
      }
    }
  }

  function yichuClick(index) {
    addForm.value.controlList.splice(index, 1)
    state.controlListcun=addForm.value.controlList
  }

  function xiugaiClick(row, index) {
    console.log(row,"大萨达卡卡修改")
    state.xiugaiPointList=row.pointList&&row.pointList.length>0?row.pointList.map(itemp=>{
      return {code:itemp}
    }):[]
    row.index = index
    let data = []
    combinationCutDto.index = index
    combinationCutDto.expression = row.controlExpression
    if (row.controlList.length > 0) {
      row.controlList.forEach(itemsub => {
        if (itemsub.controlPointDtoList) {
          itemsub.controlPointDtoList.forEach(itemc => {
            data.push(Number(itemsub.stationCode) + '.' + itemc.controlPoint)
          })
        }
      })
      state.expressStrString1 = data
      state.stationCodeList.forEach(item => {
        if (row.controlList.length > 0) {
          row.controlList.forEach(itemsub => {
            if (Number(item.value) == Number(itemsub.stationCode)) {
              item.e = itemsub.controlPointDtoList.map(iteme => {
                return Number(iteme.controlPoint)
              })
            }
          })
        }
      })
      if (row.controlList.length > 0) {
        addForm.value.stationNumber = row.controlList[0].stationCode
        addForm.value.controlPort = row.controlList[0].controlPointDtoList.map(item => {
          return Number(item.controlPoint)
        })
      }
    }
    state.yinziListHB = row.pointList.map(item => {
      return {
        code: item,
      }
    })
    state.expression = row.expression
    addForm.value.nameDtoList=row.pointList
    addForm.value.expression=row.expression
  }

  function getlistAnalogsData() {
    let data = {};
    state.querystwo.forEach((item) => {
      data[item.field] = item.binddata;
    });
    // 分站下拉数据
    axios.post('mutation/listSites', {}).then(res => {
      state.stationCodeList = Object.keys(res.data).map(key => {
        return {
          label: key + "-" + res.data[key],
          value: key,
          controlPortList: [1, 2, 3, 4, 5, 6]
        }
      })
      state.querystwo.forEach(item => {
        if (item.field == 'conditionStationCode')
          item.options = state.stationCodeList
      });
    })
  }

  function saveControlClick() {
    let data=combinationCutDto.controlList
     
    if (!addForm.value.controlList) {
      addForm.value.controlList = []
    }
    if (!addForm.value.expression) {
      ElMessage.warning('请填入公式!')
    } else if (!combinationCutDto.expression) {
      ElMessage.warning('请填入控制口!')
    } else {
      axios.post('gasProtrusionAlarm/checkExpression', {
        expression: addForm.value.expression,
        pointList: addForm.value.nameDtoList,
      }).then(res => {
        if (res.code == 200) {
          if (addForm.value.controlList.length > 0) {
            if(!isEmpty(combinationCutDto.index)){
                addForm.value.controlList[combinationCutDto.index].expression=addForm.value.expression
                addForm.value.controlList[combinationCutDto.index].pointList=addForm.value.nameDtoList
                addForm.value.controlList[combinationCutDto.index].controlExpression=combinationCutDto.expression
                addForm.value.controlList[combinationCutDto.index].controlList=data
            }else{
               addForm.value.controlList.push({
                  expression: addForm.value.expression,
                  pointList: addForm.value.nameDtoList,
                  controlExpression: combinationCutDto.expression,
                  controlList: data
                })
            }
          } else {
            addForm.value.controlList.push({
              expression: addForm.value.expression,
              pointList: addForm.value.nameDtoList,
              controlExpression: combinationCutDto.expression,
              controlList: combinationCutDto.controlList
            })
          }
          if (addForm.value.controlList.length > 1) {
            if (!addForm.value.isPublish) {
              addForm.value.controlList = [addForm.value.controlList[0]]
              ElMessage.warning('不下发状态只能配置一个表达式');
            }
          }
          state.yinziListHB=[]
          state.expression=''
          addForm.value.controlPort=[]
          state.expressStrString1=[]
          combinationCutDto.expression=undefined
          combinationCutDto.index=undefined
          state.controlListcun=addForm.value.controlList
        }
      })
    }

  }

  function checkboxChange(e) {
    state.stationCodeList.forEach(item => {
      if (addForm.value.stationNumber == item.value) {
        item.e = e
      }
    })
    // combinationCutDto.controlList=e
    combinationCutDto.controlList.forEach(itemsub => {
      if (itemsub.stationCode == addForm.value.stationNumber) {
        itemsub.controlPointDtoList = e.map(iteme => {
          return {
            controlPoint: iteme,
            fragment: Number(itemsub.stationCode) + '.' + iteme
          }
        })
      }
    })
  }
  function qingkongClick (){
    addForm.value.controlPort=[]
    state.expressStrString1=[]
    combinationCutDto.expression=undefined
  }

  function queryClick(type) {
    if (type == 2) {
      addForm.value={
        conditionStationCode:undefined,
        placeName:undefined

      }
    }
    getlistGasProtrusionNode();
  }

  function querystationCodechange(val) {
    addForm.value.controlPort=[]
    state.expressStrString1=[]
    addForm.value.stationCode = val
    addForm.value.stationNumber = val
    if (val == addForm.value.stationCodecun) {
      yinziList.value = addForm.value.nodeList
      listAnalog.value = addForm.value.nodeList
      state.expression = addForm.value.expression
      addForm.value.controlList=state.controlListcun
      getlistGasProtrusionNode()
    } else {
      if(addForm.value.isPublish==1){
        state.yinziListHB = []
        yinziList.value = []
        listAnalog.value = []
        addForm.value.controlPort = []
        combinationCutDto.expression = ''
        state.expression = ''
      }
      addForm.value.controlList=[]
      getlistGasProtrusionNode()
    }
  }

  function selectClick(row, index) {
    row.marker = true;
    row.title = row.pointId + "/" + row.sensorName;
    row.objarr = [{
        left: "编号",
        right: row.pointId,
      },
      {
        left: "名称",
        right: row.pointId + "/" + row.sensorName,
      },
      {
        left: "位置",
        right: row.placeName,
      },
    ];
    yinziList.value.push(row);
    yinziList.value = _.uniqWith(yinziList.value, _.isEqual);
    listAnalog.value.splice(index, 1)
  }
  function selectHBClick(row) {
    if (row.cutType) {
      if ((row.cutType == '上限断电' || row.cutType == '上下限断电') &&
        (isEmpty(row.upperCutValue) || isEmpty(row.upperRelieveValue)) && row.type != 'D') {
        proxy.$modal.msgWarning("请填写上限断电值或复电值");
      } else if ((row.cutType == '下限断电' || row.cutType == '上下限断电') &&
        (isEmpty(row.lowerCutValue) ||  isEmpty(row.lowerRelieveValue)) && row.type != 'D') {
        proxy.$modal.msgWarning("请填写下限断电值或复电值");
      } else if ((row.cutType == '上限断电' || row.cutType == '上下限断电')
       &&(row.upperCutValue < row.upperRelieveValue) && row.type != 'D') {
        proxy.$modal.msgWarning("上限断电值要大于等于上限复电值");
      } else if ((row.cutType == '下限断电' || row.cutType == '上下限断电') 
      &&(row.lowerCutValue > row.lowerRelieveValue) && row.type != 'D') {
        proxy.$modal.msgWarning("下限断电值要小于等于下限复电值");
      } else if (row.cutType == '上下限断电'&&(row.upperCutValue <= row.lowerCutValue)
       && row.type != 'D') {
        proxy.$modal.msgWarning("上限断电值要大于下限断电值");
      } else if (row.cutType == '上下限断电'&&(row.upperRelieveValue <= 
      row.lowerRelieveValue) && row.type != 'D') {
        proxy.$modal.msgWarning("上限复电值要大于下限复电值");
      } else if (row.sensorName == '瓦斯' &&
        !row.mutationThreshold && row.type != 'D') {
        proxy.$modal.msgWarning("请填写突变门限");
      } else if (row.sensorName == '瓦斯' &&
        !row.rise && row.type != 'D') {
        proxy.$modal.msgWarning("请填写涨幅");
      } else if (row.sensorName == '瓦斯' &&
        !row.timeScale && row.type != 'D') {
        proxy.$modal.msgWarning("请填写时间范围");
      } else {
        row.name = row.sensorName
        row.code = row.pointId
        row.place = row.placeName
        state.yinziListHB.push(row);
      }
    } else {
      proxy.$modal.msgWarning("请选择断电模式");
    }

  }

  function removeHBClick(index) {
    yinziList.value.splice(index, 1)
  }
  function cardListremove(data){
    listAnalog.value=[data].concat(listAnalog.value)
  }

  function deleteBtn(row) {
    let ids = [];
    if (row != "mul") {
      ids = [row.id];
    } else {
      if (store.records.length > 0) {
        ids = store.records.map((item) => {
          return item.id;
        });
      } else {
        proxy.$modal.msgWarning("请先勾选所要删除数据!");
      }
    }
    if (ids.length > 0) {
      ElMessageBox.confirm(
          '确定要删除该数据吗?',
          '温馨提示', {
            confirmButtonText: '确定',
            cancelButtonText: '取消',
            type: 'warning',
          }
        )
        .then(() => {
          axios.post(state.options.delUrl, ids).then(res => {
            if (res.code == 200) {
              proxy.$refs['zujianname'].getData()
              state.addModal = false
              ElMessage({
                type: 'success',
                message: res.msg,
              })
              store.records = []
            }
          })

        })
        .catch(() => {
          ElMessage({
            type: 'info',
            message: '删除取消',
          })
        })
    }
  }
  getlistAnalogsData();
</script>
<style lang="scss" scoped>
  .p_title {
    font-size: 16px;
    font-weight: 700;
    padding: 0 10px;
  }

  .pagestyle {
    overflow-y: auto;

    .addBox {
      padding: 42px 0;
      border-radius: 8px;
      background: rgba(255, 255, 255, 0.8);
    }

    .remarkBox {
      padding: 10px;
      font-size: 14px;
    }

    .btnSubmit {
      margin-top: 40px;
      text-align: center;
    }
  }
</style>